package com.zr.legalperson.controller;

import com.zr.legalperson.model.LegalPerson;
import com.zr.legalperson.model.Plant;
import com.zr.legalperson.model.PlantSelectVo;
import com.zr.legalperson.model.vo.PlantAddVo;
import com.zr.legalperson.model.vo.PlantResultVo;
import com.zr.legalperson.model.vo.PlantUpdateStatusVo;
import com.zr.legalperson.model.vo.PlantUpdateVo;
import com.zr.legalperson.service.LegalPersonService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2019/1/3.
 */
@RestController
public class LegalPersonController {

    @Autowired
    private LegalPersonService legalPersonService;
    @GetMapping("legalPerson/ofCurrentUser")
    @CrossOrigin
    public ResultVO<List<LegalPerson>>legalPersonDropDownValue(){
        return legalPersonService.legalPersonDropDownValue();
    }

    @PostMapping("legalPlant/query")
    @CrossOrigin
    public ResultVO<AllRecords>legalPlantQuery(@RequestBody PlantSelectVo plantSelectVo){
        return legalPersonService.legalPlantQuery(plantSelectVo);
    }


    @PostMapping("legalPlant/add")
    @CrossOrigin
    public ResultVO<PlantAddVo>legalPlantAdd(@RequestBody @Valid PlantAddVo plantAddVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.legalPlantAdd(plantAddVo);
    }

    @PostMapping("legalPlant/updatestatus")
    @CrossOrigin
    public ResultVO<PlantUpdateStatusVo> legalPlantUpdatestatus(@RequestBody @Valid PlantUpdateStatusVo updateStatusVo, BindingResult bindingResult){

        System.out.println(updateStatusVo);
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.legalPlantUpdatestatus(updateStatusVo);
    }

    @PostMapping("legalPlant/update")
    @CrossOrigin
    public ResultVO<PlantUpdateVo> legalPlantUpdate(@RequestBody @Valid PlantUpdateVo plantUpdateVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.legalPlantUpdate(plantUpdateVo);
    }


}
